﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using plust.Persistencia;
using plust.Clases;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using FirstFloor.ModernUI.Windows.Controls;
using FirstFloor.ModernUI.Windows;
using System.Threading;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for ModificarVenta.xaml
    /// </summary>
    public partial class ModificarVenta : UserControl, IContent
    {
        private PersistenciaDB persistencia = new PersistenciaDB();

        public ModificarVenta()
        {
            InitializeComponent();
        }

        private async void cargarVentas()
        {
            this.pb.IsIndeterminate = true;

            this.dgModificarVentas.Visibility = System.Windows.Visibility.Hidden;

            this.pb.Visibility = System.Windows.Visibility.Visible;

            var slowTask = Task<DataTable>.Factory.StartNew(() => cargarVentasTask());

            await slowTask;

            this.pb.Visibility = System.Windows.Visibility.Hidden;

            this.pb.IsIndeterminate = false;

            this.dgModificarVentas.ItemsSource = slowTask.Result.DefaultView;

            this.dgModificarVentas.Visibility = System.Windows.Visibility.Visible;
        }

        private DataTable cargarVentasTask()
        {
            return persistencia.obtenerVentas();
        }

        private void dgModificarVentas_Row_DoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.dgModificarVentas.SelectedItem != null)
            {
                  
                try
                {
                    int id_venta = int.Parse(((DataRowView)this.dgModificarVentas.SelectedItem).Row["id_venta"].ToString());
                    string fecha = ((DataRowView)this.dgModificarVentas.SelectedItem).Row["fecha_venta"].ToString();
                    int monto =  int.Parse(((DataRowView)this.dgModificarVentas.SelectedItem).Row["monto_venta"].ToString());
                    int tipo = int.Parse(((DataRowView)this.dgModificarVentas.SelectedItem).Row["tipo_venta"].ToString());
                    int id_fact = int.Parse(((DataRowView)this.dgModificarVentas.SelectedItem).Row["id_factura"].ToString());
                    int id_bol = int.Parse(((DataRowView)this.dgModificarVentas.SelectedItem).Row["id_boleta"].ToString());

                    Venta v = new Venta(id_venta, fecha, monto,tipo,id_fact,id_bol);
                    
                    VerDetalleVenta vdv = new VerDetalleVenta(v);

                    var wnd = new ModernWindow
                    {
                        Style = (Style)App.Current.Resources["EmptyWindow"],
                        Content = vdv,
                        Width = 600,
                        Height = 480,
                        Margin = new Thickness(42),
                        Padding = new Thickness(42),
                        WindowStartupLocation = WindowStartupLocation.CenterScreen,
                        ResizeMode = ResizeMode.NoResize
                    };
                    
                    wnd.ShowDialog();

                    this.cargarVentas();

                }

                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        public void OnFragmentNavigation(FirstFloor.ModernUI.Windows.Navigation.FragmentNavigationEventArgs e)
        {
            
        }

        public void OnNavigatedFrom(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
           
        }

        public void OnNavigatedTo(FirstFloor.ModernUI.Windows.Navigation.NavigationEventArgs e)
        {
            this.cargarVentas();
        }

        public void OnNavigatingFrom(FirstFloor.ModernUI.Windows.Navigation.NavigatingCancelEventArgs e)
        {
           
        }
    }
}
